package com.eduardcapell.lopodder.misc;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.Window;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.eduardcapell.lopodder.beans.Feed;


public class Utils {

	private Utils() {
		// Private, non-instantiable constructor.
	}
	

	public static String dateTime(Date date, String format) {
		DateFormat df = new SimpleDateFormat(format);
		return df.format(date);
	}
	
	
	public static String formatMB(long n) {
		NumberFormat nf = new DecimalFormat(Constants.MB_FORMAT);
		return nf.format(n / Math.pow(2, 20));
	}
	
	
	public static boolean feedExists(Feed f, List<Feed> feeds) {
		return feeds!=null && feeds.contains(f);
	}
	
	
	public static void centerWindow(Window w) {
		int height = w.getHeight();
		int width = w.getWidth();
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		Dimension screenSize = toolkit.getScreenSize();
		
		int x = (screenSize.width - width) / 2;
		int y = (screenSize.height - height) / 2;
		
		w.setLocation(x, y);
	}
	
	
	public static String replaceCharactersInFileName(String fileName) {
		Map<String, String> chars = new HashMap<String, String>();
		chars.put(" ", "_");
		chars.put("%20", "_");
		chars.put("\\(", "_");
		chars.put("\\)", "_");
		chars.put("\\\\", "_");


		Set<Map.Entry<String, String>> set = chars.entrySet();
		Iterator<Map.Entry<String, String>> it = set.iterator();
		while (it.hasNext()) {
			Map.Entry<String, String> me = it.next();
			fileName = fileName.replaceAll(me.getKey(), me.getValue()); 
		}
		
		while (fileName.indexOf("__") >= 0) {
			fileName = fileName.replaceAll("__", "_");	
		}
		
		while (fileName.endsWith("_")) {
			fileName = fileName.substring(0, fileName.length() - 1);
		}
		
		return fileName;
	}

    public static String microsecondsToHoursMinsSeconds(long m) {
        String s = null;
        long ms = m / 1000;
        long seconds = ms / 1000;
        int hours = (int) seconds / 3600;
        int secsOver = (int) seconds % 3600;
        int minutes = secsOver / 60;
        secsOver = secsOver % 60;

        NumberFormat nf = new DecimalFormat("00");
        s = nf.format(hours) + ":" + nf.format(minutes) + ":" +
            nf.format(secsOver);

        return s;
    }


    public static void main(String[] args) {
        long m = 3600000000L;
        System.out.println(microsecondsToHoursMinsSeconds(m));
    }
}
